package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 保存菜品
     * @param dishDTO
     */
    @Override
    @Transactional          //事务控制，必须保存菜品添加和口味添加都成功
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //插入菜品数据
        dishMapper.insert(dish);

        //插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dishId = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询
     *
     * @param DishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO DishPageQueryDTO) {
        PageHelper.startPage(DishPageQueryDTO.getPage(), DishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(DishPageQueryDTO.getCategoryId(),
                DishPageQueryDTO.getName(), DishPageQueryDTO.getStatus());
        long total = page.getTotal();       //总记录
        List<DishVO> pageResult = page.getResult();  //记录
        return new PageResult(total, pageResult);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(String[] ids) {
        //判断能不能删除？起售中的菜品、被套餐关联的菜品不能被删除
        for (String id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        List<Long> setmealDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealDishIds != null && setmealDishIds.size() > 0) {
            //有关联是不能删除的
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //批量删除菜品表中的数据
        dishMapper.deleteBatch(ids);
        //删除菜品对应的口味数据
        dishFlavorMapper.deleteBatchByDishIds(ids);
    }

    @Override
    public DishVO getDishById(Integer id) {
        //根据菜品id获取菜品
        Dish dish = dishMapper.getById(id.toString());
        //根据菜品id获取口味数据
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getFlavorByDishId(id);
        //封装
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        //更新菜品表数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //删除原有的口味
        String dishId = String.valueOf(dishDTO.getId());
        String[] dishIds = new String[]{dishId};
        dishFlavorMapper.deleteBatchByDishIds(dishIds);
        //更新
        //插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dishId1 = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dishId1));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<Dish> getDishByCategoryId(Integer categoryId) {
        List<Dish> dishes = dishMapper.selectByCId(categoryId);
        return dishes;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        //查询出菜品，根据分类id和status
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorByDishId(d.getId().intValue());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}